
#include <stdint.h>
#include <proc/p32mx575f256h.h>
#include "system_config.h"
#include "system_definitions.h"
#include "IIC_24w256.h"

#define	SLAVE_R_ADDR	0XAF
#define	SLAVE_W_ADDR	0XAE

#define	WP_OPEN()	PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_8);
#define	WP_SHUT()	PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_8);

typedef struct{
	uint8_t slave;

}IIC_24w256_Block_T;


//延时函数
void delay_I2C1(uint16_t num)
{
	while(num--);

        return;
}
//延时函数
void delay_I2C2(uint16_t num)
{
	while(num--);

        return;
}
//函数功能：打开E2PROM（24C64）的写保护
void open_wp(void)
{
	delay_I2C1(2);
        PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_8);
//	WP = 1;

        return;

}
//函数功能：关闭E2PROM（24C64）的写保护
void close_wp(void)
{

//	WP = 0;
        PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_8);
	delay_I2C1(2);

        return;
}

//函数功能：使能主器件接收模式，读取数据
//输入参数：无
//返回参数：无
uint8_t master_readI2C2(void)
{
	I2C1CONbits.RCEN = 1; //主器件接收使能
	while(I2C1CONbits.RCEN);
	I2C1STATbits.I2COV = 0;
	return(I2C1RCV);
}

//函数功能：写数据至输出寄存器
//输入参数：无
//返回参数：无
char master_writeI2C2(uint8_t data)
{
	//success_flag = 0;
	I2C1TRN = data;
	if(I2C1STATbits.IWCOL) //如果写冲突发生，返回-1
	{
		I2C1STATbits.IWCOL=0;
		return -1;
	}
	else
	{
		return 0;
	}
}

//函数功能：发出重复启动信号
//输入参数：无
//返回参数：无
void restart_I2C2(void)
{

	I2C1CONbits.RSEN = 1;

        return;

}

//函数功能：发出启动信号
//输入参数：无
//返回参数：无
void start_I2C2(void)
{

	I2C1CONbits.SEN = 1;

        return;

}

//函数功能：产生停止条件
//输入参数：无
//返回参数：无
void stop_I2C2(void)
{

	I2C1CONbits.PEN = 1;

        return;

}

//函数功能：启动应答事件，发送NACK
//输入参数：无
//返回参数：无
void NotAck_I2C2(void)
{
	I2C1CONbits.ACKDT = 1;
	I2C1CONbits.ACKEN = 1;

        return;
}

//函数功能：启动应答事件，发送ACK
//输入参数：无
//返回参数：无
void AckI2C2(void)
{
	I2C1CONbits.ACKDT = 0;
	I2C1CONbits.ACKEN = 1;

        return;
}




//函数功能：打开第2路I2C
//输入参数：无
//返回参数：无
void open_I2C1(void)
{
    I2C1CONbits.ON = 1;//打开I2C模块
    asm("nop");
    asm("nop");

    return;
}

//函数功能：关闭第2路I2C
//输入参数：无
//返回参数：无
void close_I2C1(void)
{
    asm("nop");
    asm("nop");
    I2C1CONbits.ON = 0;//禁止I2C模块

    return;
}


//函数功能：发出启动信号
//输入参数：无
//返回参数：无
void start_I2C1(void)
{

	I2C1CONbits.SEN = 1;

        return;

}

//函数功能：使能主器件接收模式，读取数据
//输入参数：无
//返回参数：无
uint8_t master_readI2C1(void)
{
	I2C1CONbits.RCEN = 1; //主器件接收使能
	while(I2C1CONbits.RCEN);
	I2C1STATbits.I2COV = 0;
	return(I2C1RCV);
}

//函数功能：写数据至输出寄存器
//输入参数：无
//返回参数：无
char master_writeI2C1(uint8_t data)
{
	//success_flag = 0;
	I2C1TRN = data;
	if(I2C1STATbits.IWCOL) //如果写冲突发生，返回-1
	{
		I2C1STATbits.IWCOL=0;
		return -1;
	}
	else
	{
		return 0;
	}
}

//函数功能：发出重复启动信号
//输入参数：无
//返回参数：无
void restart_I2C1(void)
{

	I2C1CONbits.RSEN = 1;

        return;

}

//延时函数，时间基数为：1/FCY
void delay_time(uint32_t num)
{
	uint16_t i;
	for(i+0;i<num;i++)
	{
		asm("nop");
	}

        return;
}



//函数功能：产生停止条件
//输入参数：无
//返回参数：无
void stop_I2C1(void)
{

	I2C1CONbits.PEN = 1;

        return;

}

//函数功能：启动应答事件，发送NACK
//输入参数：无
//返回参数：无
void NotAck_I2C1(void)
{
	I2C1CONbits.ACKDT = 1;
	I2C1CONbits.ACKEN = 1;

        return;
}

//函数功能：启动应答事件，发送ACK
//输入参数：无
//返回参数：无
void AckI2C1(void)
{
	I2C1CONbits.ACKDT = 0;
	I2C1CONbits.ACKEN = 1;

        return;
}


//函数功能：页写，每次写最多可以写16个字节
//输入参数：device_addr:器件地址，I2C_addr:地址位，*s：字节指针，指向数组首地址，num：写入字节数目
//返回参数：无
uint8_t page_write_E2PROM(uint8_t device_addr,uint8_t I2C_addr_h, uint8_t I2C_addr_l,uint8_t *s,uint8_t num)
{
	close_wp();
	uint8_t i;
	start_I2C2(); 				//发出启动信号
	delay_I2C2(5);
	while(I2C1CONbits.SEN);
	master_writeI2C2(device_addr); 		//发送器件地址  写
	while(I2C1STATbits.TRSTAT);
	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
	 	return 0;
	delay_I2C2(5);
	master_writeI2C2(I2C_addr_h);  //发出地址位高8位
	while(I2C1STATbits.TRSTAT);
	if(I2C1STATbits.ACKSTAT)
		return 0;
	delay_I2C2(5);
	master_writeI2C2(I2C_addr_l);  //发出地址位低8位
	while(I2C1STATbits.TRSTAT);
	if(I2C1STATbits.ACKSTAT)
		return 0;
	delay_I2C2(5);
	for(i=0;i<num;i++,s++)
	{
		master_writeI2C2(*s);  //发出8位数据
		while(I2C1STATbits.TRSTAT);
		if(I2C1STATbits.ACKSTAT)
			return 0;
		delay_I2C2(5);
	}
	stop_I2C2(); 		//产生停止条件
	while(I2C1CONbits.PEN);
	open_wp();
	return 1;
}

//函数功能：连续读，读取字节数为num
//输入参数：device_addr:器件地址，I2C_addr:地址位，*s,字节指针，指向数组首地址，用于保存读取到的数据，num：读取字节数目
//返回参数：无
uint8_t read_conitue(uint8_t device_addr,uint8_t I2C_addr_h,uint8_t I2C_addr_l,uint8_t *s,uint8_t num)
{

	uint8_t i;

        close_I2C1();//关闭I2C2
        open_I2C1(); //打开I2C2
        delay_time(10000);
   	close_wp();


	start_I2C2();
	while(I2C1CONbits.SEN);

	delay_I2C2(5);

	master_writeI2C2(device_addr); //发送器件地址，写
	while(I2C1STATbits.TRSTAT);
	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
		return false;

	delay_I2C2(5);

	master_writeI2C2(I2C_addr_h); //发出地址位高8位
	while(I2C1STATbits.TRSTAT);
	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
		return false;
	delay_I2C2(5);

	master_writeI2C2(I2C_addr_l); //发出地址位低8位
	while(I2C1STATbits.TRSTAT);
	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
		return false;


	restart_I2C2();
	while(I2C1CONbits.RSEN);

	delay_I2C2(5);

	master_writeI2C2((device_addr|0x01)); //发送器件地址，读
	while(I2C1STATbits.TRSTAT);
	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
		return false;

	delay_I2C2(5);

	for(i=0;i<num;i++,s++)
	{
		*s = master_readI2C2();
		if(i!= (num-1))
		{
			AckI2C2();
		}
		delay_I2C2(50);

	}
	NotAck_I2C2();
	stop_I2C2();
	while(I2C1CONbits.PEN);
	open_wp();

        return true;
}


////函数功能：连续读，读取字节数为num
////输入参数：device_addr:器件地址，I2C_addr:地址位，*s,字节指针，指向数组首地址，用于保存读取到的数据，num：读取字节数目
////返回参数：无
//void read_conitue(uint8_t device_addr,uint8_t I2C_addr_h,uint8_t I2C_addr_l,uint8_t *s,uint8_t num)
//{
//	uint8_t i;
//
//	close_I2C1();//关闭I2C1
//	open_I2C1(); //打开I2C1
////	delay_time(10000);
//
//        PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_8);
////	close_wp();
//
//	start_I2C1();
//	while(I2C1CONbits.SEN);
//
//	delay_I2C1(5);
//
//	master_writeI2C1(device_addr); //发送器件地址，写
//	while(I2C1STATbits.TRSTAT);
//	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
//		return ;
//
//	delay_I2C1(5);
//
//	master_writeI2C1(I2C_addr_h); //发出地址位高8位
//	while(I2C1STATbits.TRSTAT);
//	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
//	 	return ;
//	delay_I2C1(5);
//
//	master_writeI2C1(I2C_addr_l); //发出地址位低8位
//	while(I2C1STATbits.TRSTAT);
//	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
//	 	return ;
//
//
//	restart_I2C1();
//	while(I2C1CONbits.RSEN);
//
//	delay_I2C1(5);
//
//	master_writeI2C1((device_addr|0x01)); //发送器件地址，读
//	while(I2C1STATbits.TRSTAT);
//	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
//	 	return ;
//
//	delay_I2C1(5);
//
//	for(i=0;i<num;i++,s++)
//	{
//		s[i] = master_readI2C1();
//		if(i!= (num-1))
//		{
//			AckI2C1();
//		}
//		delay_I2C1(50);
//
//	}
//	NotAck_I2C1();
//	stop_I2C1();
//	while(I2C1CONbits.PEN);
////	open_wp();
//        PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_8);
//
//        return;
//}



#if 0

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  iic_24w256_write_byte
 *  Description:  
 * =====================================================================================
 */
void iic_24w256_write_byte(uint8_t slave, uint16_t addr, uint8_t data){

    uint8_t s_addr = (((slave << 1) & 0x0E) | 0xAE);

	WP_SHUT();
//        DRV_I2C0_DeInitialize();
//        DRV_I2C0_Initialize();
//        DRV_I2C0_MasterRestart();

        delay_some(5);
	DRV_I2C0_MasterStart();
	DRV_I2C0_WaitForStartComplete();
        delay_some(5);

	DRV_I2C0_ByteWrite(s_addr);
	DRV_I2C0_WaitForByteWriteToComplete();
	DRV_I2C0_WriteByteAcknowledged();
        delay_some(5);

	DRV_I2C0_ByteWrite((addr >> 8) & 0x00ff);
	DRV_I2C0_WaitForByteWriteToComplete();
	DRV_I2C0_WriteByteAcknowledged();
        delay_some(5);

	DRV_I2C0_ByteWrite((addr >> 0) & 0x00ff);
	DRV_I2C0_WaitForByteWriteToComplete();
	DRV_I2C0_WriteByteAcknowledged();
        delay_some(5);

	DRV_I2C0_ByteWrite(data);
	DRV_I2C0_WaitForByteWriteToComplete();
	DRV_I2C0_WriteByteAcknowledged();
        delay_some(5);

	DRV_I2C0_MasterStop();

	WP_OPEN();

	return ;
}		/* -----  end of function iic_24w256_write_byte  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  iic_24w256_read_byte
 *  Description:  
 * =====================================================================================
 */
uint8_t iic_24w256_read_byte(uint8_t slave){

	uint8_t ret = 0;

	WP_SHUT();
//        DRV_I2C0_DeInitialize();
//        DRV_I2C0_Initialize();
//        DRV_I2C0_MasterRestart();
	delay_some(5);

	DRV_I2C0_MasterStart();
	DRV_I2C0_WaitForStartComplete();
	delay_some(5);
	DRV_I2C0_ByteWrite((slave << 1) & 0xAF);
	DRV_I2C0_WaitForByteWriteToComplete();
	delay_some(5);
	if(DRV_I2C0_WriteByteAcknowledged()){
		//	DRV_I2C0_SetUpByteRead();                      //Set Rx enable in MSTR which causes SLAVE to send data
		//	while(!DRV_I2C0_WaitForReadByteAvailable());   // Wait till RBF = 1;                                    Which means data is available in I2C1RCV reg
		ret = DRV_I2C0_ByteRead();                     //Read from I2CxRCV
		DRV_I2C0_MasterNACKSend();                     //last byte;                                             send NACK to Slave, no more data needed
		while (!DRV_I2C0_WaitForACKOrNACKComplete());  // wait till NACK sequence is complete i.e ACKEN = 0
		delay_some(5);
		DRV_I2C0_MasterStop();
		WP_OPEN();
	}else{
		ret = 0;
	}

	return ret;
}		/* -----  end of function iic_24w256_read_byte  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  iic_24w256_random_read
 *  Description:  读取一个地址的数据。
 * =====================================================================================
 */
uint8_t iic_24w256_random_read(uint8_t slave, uint16_t addr){

	uint8_t ret = 0;
        uint8_t s_addr = (((slave << 1) & 0x0E) | 0xAE);

	WP_SHUT();
        DRV_I2C0_DeInitialize();
        DRV_I2C0_Initialize();
//        DRV_I2C0_MasterRestart();

	DRV_I2C0_MasterStart();
	DRV_I2C0_WaitForStartComplete();
	delay_some(5);

	DRV_I2C0_ByteWrite(s_addr);
//	DRV_I2C0_ByteWrite(0xAE);
	DRV_I2C0_WaitForByteWriteToComplete();
	delay_some(5);
	if(DRV_I2C0_WriteByteAcknowledged()){
		DRV_I2C0_ByteWrite((addr >> 8) & 0x00ff);
		DRV_I2C0_WaitForByteWriteToComplete();
		ret = DRV_I2C0_WriteByteAcknowledged();
		delay_some(5);

		DRV_I2C0_ByteWrite((addr >> 0) & 0x00ff);
		DRV_I2C0_WaitForByteWriteToComplete();
		ret = DRV_I2C0_WriteByteAcknowledged();
		delay_some(5);

		DRV_I2C0_MasterRestart();
//		DRV_I2C0_MasterStart();
		DRV_I2C0_WaitForStartComplete();
		delay_some(5);

		DRV_I2C0_ByteWrite(s_addr | 0x01);
//		DRV_I2C0_ByteWrite(0xAE | 0X01);
		DRV_I2C0_WaitForByteWriteToComplete();
		ret = DRV_I2C0_WriteByteAcknowledged();

		DRV_I2C0_SetUpByteRead();                   //Set Rx enable in MSTR which causes SLAVE to send data
		while(!DRV_I2C0_WaitForReadByteAvailable());        // Wait till RBF = 1; Which means data is available in I2C1RCV reg
		ret = DRV_I2C0_ByteRead();   //Read from I2CxRCV
		DRV_I2C0_MasterNACKSend();                     //last byte; send NACK to Slave, no more data needed
		while (!DRV_I2C0_WaitForACKOrNACKComplete());  // wait till NACK sequence is complete i.e ACKEN = 0
		DRV_I2C0_MasterStop();

//		delay_some(5);
//                ret = DRV_I2C0_SetUpByteRead();
//       		ret = DRV_I2C0_WaitForReadByteAvailable();
//		if(ret == true){
//			ret = DRV_I2C0_ByteRead();                     //Read from I2CxRCV
//			DRV_I2C0_MasterNACKSend();                     //last byte; send NACK to Slave, no more data needed
//			while (!DRV_I2C0_WaitForACKOrNACKComplete());  // wait till NACK sequence is complete i.e ACKEN = 0
//			DRV_I2C0_MasterStop();
//			delay_some(5);
//		}else{
//			delay_some(5);
//		}
		WP_OPEN();
	}else{
		ret = 0;
	}
	return ret;
}		/* -----  end of function iic_24w256_random_read  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  24w256_read_data
 *  Description:  从24w256中读取数据。
 * =====================================================================================
 */
void iic_24w256_read_data(uint8_t slave, uint16_t addr, uint16_t nb, uint16_t *data){
	
	uint16_t i;
	uint8_t byte_h;
	uint8_t byte_l;

	WP_SHUT();
	DRV_I2C0_MasterStart();
	DRV_I2C0_WaitForStartComplete();

	DRV_I2C0_ByteWrite((slave << 1) & 0xAE);
	DRV_I2C0_WaitForByteWriteToComplete();
	DRV_I2C0_WriteByteAcknowledged();

        DRV_I2C0_ByteWrite(addr);
	DRV_I2C0_WaitForByteWriteToComplete();
	DRV_I2C0_WriteByteAcknowledged();

	DRV_I2C0_MasterRestart();
//	DRV_I2C0_WaitForStartComplete();

	DRV_I2C0_ByteWrite((slave << 1) & 0xAF);
	DRV_I2C0_WaitForByteWriteToComplete();
	DRV_I2C0_WriteByteAcknowledged();

	for ( i = 0; i < nb; i += 1 ) {
//                    DRV_I2C0_SetUpByteRead();                   //Set Rx enable in MSTR which causes SLAVE to send data
//                    while(!DRV_I2C0_WaitForReadByteAvailable());        // Wait till RBF = 1; Which means data is available in I2C1RCV reg
                    byte_h = DRV_I2C0_ByteRead();   //Read from I2CxRCV
		    DRV_I2C0_MasterACKSend();               //Send ACK to Slave
                    while (!DRV_I2C0_WaitForACKOrNACKComplete());

//                    DRV_I2C0_SetUpByteRead();                   //Set Rx enable in MSTR which causes SLAVE to send data
//                    while(!DRV_I2C0_WaitForReadByteAvailable());        // Wait till RBF = 1; Which means data is available in I2C1RCV reg
                    byte_l = DRV_I2C0_ByteRead();   //Read from I2CxRCV
		    if(i != (nb - 1)){
			    DRV_I2C0_MasterACKSend();               //Send ACK to Slave
		    }
                    while (!DRV_I2C0_WaitForACKOrNACKComplete());
		    data[i] = byte_h;
		    data[i] = (data[i] << 8) & 0xff00;
		    data[i] += byte_l;
	}
	DRV_I2C0_MasterNACKSend();                      //last byte; send NACK to Slave, no more data needed
	while (!DRV_I2C0_WaitForACKOrNACKComplete());   // wait till NACK sequence is complete i.e ACKEN = 0
	DRV_I2C0_MasterStop();
//                DRV_I2C0_WaitForStopComplete();
	DRV_I2C0_DeInitialize();
	WP_OPEN();

	return ;
}		/* -----  end of function 24w256_read_data  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  24w256_write_data
 *  Description:  向24w256中写入数据。
 * =====================================================================================
 */
void iic_24w256_write_data(uint8_t slave, uint16_t addr, uint16_t nb, uint16_t *data){
	
	uint16_t i;

	WP_SHUT();

	if(DRV_I2C0_MasterBusIdle()){
		DRV_I2C0_MasterStart();
		DRV_I2C0_WaitForStartComplete();
		if(DRV_I2C0_ByteWrite((slave << 1) & 0xAE)){
			DRV_I2C0_WaitForByteWriteToComplete();
			if (DRV_I2C0_WriteByteAcknowledged()){
				for(i = 0; i < (nb+1); i++){
					if (i == 0){
						DRV_I2C0_ByteWrite(addr);
					}else{
						DRV_I2C0_ByteWrite(data[i-1]);
					}
					DRV_I2C0_WaitForByteWriteToComplete();
				}
				DRV_I2C0_MasterStop();
//				DRV_I2C0_WaitForStopComplete();
				WP_OPEN();
				return;
			}else{
				DRV_I2C0_DeInitialize();
				WP_OPEN();
				return;
			}
		}else{
			WP_OPEN();
			return;
		}

	}else{
		WP_OPEN();
		return;
	}
}		/* -----  end of function 24w256_write_data  ----- */

//函数功能：页写，每次写最多可以写16个字节
//输入参数：device_addr:器件地址，I2C_addr:地址位，*s：字节指针，指向数组首地址，num：写入字节数目
//返回参数：无
//uint8 page_write_E2PROM(uint8 device_addr,uint8 I2C_addr_h,uint8 I2C_addr_l,uint8 *s,uint8 num)
//{
//	close_wp();
//	uint8 i;
//	start_I2C2(); 				//发出启动信号
//	delay_I2C2(5);
//	while(I2C2CONbits.SEN);
//	master_writeI2C2(device_addr); 		//发送器件地址  写
//	while(I2C2STATbits.TRSTAT);
//	if(I2C2STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
//	 	return 0;
//	delay_I2C2(5);
//	master_writeI2C2(I2C_addr_h);  //发出地址位高8位
//	while(I2C2STATbits.TRSTAT);
//	if(I2C2STATbits.ACKSTAT)
//		return 0;
//	delay_I2C2(5);
//	master_writeI2C2(I2C_addr_l);  //发出地址位低8位
//	while(I2C2STATbits.TRSTAT);
//	if(I2C2STATbits.ACKSTAT)
//		return 0;
//	delay_I2C2(5);
//	for(i=0;i<num;i++,s++)
//	{
//		master_writeI2C2(*s);  //发出8位数据
//		while(I2C2STATbits.TRSTAT);
//		if(I2C2STATbits.ACKSTAT)
//			return 0;
//		delay_I2C2(5);
//	}
//	stop_I2C2(); 		//产生停止条件
//	while(I2C2CONbits.PEN);
//	open_wp();
//	return 1;
//}
#endif


void delay_some(unsigned int delay)
{
	while (delay > 0){
		delay--;
		asm("nop");
	}
}


/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  fm24w256_single_byte_write
 *  Description:  写入一个字节 
 * =====================================================================================
 */
void fm24w256_single_byte_write(uint8_t slave, uint16_t addr, uint8_t data){

        uint8_t slave_temp = (slave << 1) & 0x0e + 0xa0;
//        DRV_I2C0_DeInitialize();
//        DRV_I2C0_Initialize();

        I2C1CONbits.ON = 0;
//        delay_some_iic(5);
        I2C1CONbits.ON = 1;
	WP_SHUT();

	DRV_I2C0_MasterStart();
	DRV_I2C0_WaitForStartComplete();
//delay_some(5);
	DRV_I2C0_ByteWrite(slave_temp);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//delay_some(5);
	DRV_I2C0_ByteWrite((addr >> 8) & 0x00ff);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//delay_some(5);
	DRV_I2C0_ByteWrite((addr >> 0) & 0x00ff);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//delay_some(5);
	DRV_I2C0_ByteWrite(data);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//delay_some(5);
	DRV_I2C0_MasterStop();

	WP_OPEN();

	return ;
}		/* -----  end of function fm24w256_single_byte_write  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  fm24w256_multiple_byte_write
 *  Description:  多字节写入
 * =====================================================================================
 */
void fm24w256_multiple_byte_write(uint8_t slave, uint16_t addr, uint16_t nb, uint8_t *data){

	uint16_t i;

//	DRV_I2C0_DeInitialize();
//	DRV_I2C0_Initialize();
//         DRV_I2C0_MasterRestart();
//        delay_some(5);

	WP_SHUT();
//        delay_some(5);
	DRV_I2C0_MasterStart();
//        delay_some(5);
	DRV_I2C0_WaitForStartComplete();
//delay_some(5);
	DRV_I2C0_ByteWrite(slave);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//delay_some(5);
	DRV_I2C0_ByteWrite((addr >> 8) & 0x00ff);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//delay_some(5);
	DRV_I2C0_ByteWrite((addr >> 0) & 0x00ff);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//delay_some(5);
	for ( i = 0; i < nb; i += 1 ) {
		DRV_I2C0_ByteWrite(data[i]);
//                delay_some(5);
		DRV_I2C0_WaitForByteWriteToComplete();
		DRV_I2C0_WriteByteAcknowledged();
//                delay_some(5);
	}

	DRV_I2C0_MasterStop();

	WP_OPEN();

	return ;
}		/* -----  end of function fm24w256_multiple_byte_write  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  fm24w256_current_addr_read
 *  Description:  读取当前地址的数据
 * =====================================================================================
 */
uint8_t fm24w256_current_addr_read(uint8_t slave){

	uint8_t data;

//        DRV_I2C0_DeInitialize();
//	DRV_I2C0_Initialize();

        PLIB_PORTS_PinClear(PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_8);
//	WP_SHUT();
        delay_some(5);

	DRV_I2C0_MasterStart();
	DRV_I2C0_WaitForStartComplete();

	DRV_I2C0_ByteWrite(0xAF);
//        DRV_I2C0_ByteWrite(slave | 0x01);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
        delay_some(5);
//        while(!DRV_I2C0_WaitForReadByteAvailable());
	data = DRV_I2C0_ByteRead();
	DRV_I2C0_MasterNACKSend();                     //last byte; send NACK to Slave, no more data needed
        delay_some(5);
	DRV_I2C0_WaitForACKOrNACKComplete();  		// wait till NACK sequence is complete i.e ACKEN = 0

	DRV_I2C0_MasterStop();

//	WP_OPEN();
        PLIB_PORTS_PinSet(PORTS_ID_0, PORT_CHANNEL_D, PORTS_BIT_POS_8);

	return data;
}		/* -----  end of function fm24w256_current_addr_read  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  fm24w256_sequential_read
 *  Description:  读取当前地址的多个数据。
 * =====================================================================================
 */
void fm24w256_sequential_read(uint8_t slave, uint16_t nb, uint8_t *data){

	uint16_t i;

	WP_SHUT();

	DRV_I2C0_MasterStart();
	DRV_I2C0_WaitForStartComplete();

	DRV_I2C0_ByteWrite(slave | 0x01);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();

	for ( i = 0; i < nb; i += 1 ) {
		data[i] = DRV_I2C0_ByteRead();
		if(i != (nb - 1)){
			DRV_I2C0_WriteByteAcknowledged();
		}else{
			DRV_I2C0_MasterNACKSend();                     //last byte; send NACK to Slave, no more data needed
		}

	}
	DRV_I2C0_WaitForACKOrNACKComplete();  		// wait till NACK sequence is complete i.e ACKEN = 0

	DRV_I2C0_MasterStop();

	WP_OPEN();

	return ;
}		/* -----  end of function fm24w256_sequential_read  ----- */

/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  fm24w256_selective_read
 *  Description:  读取选择地址的数据。
 * =====================================================================================
 */
void fm24w256_selective_read(uint8_t slave, uint16_t addr, uint16_t nb, uint8_t *data){

	uint16_t i;
        uint16_t slave_temp = (slave << 1) & 0x0e + 0xa0;

//	DRV_I2C0_DeInitialize();
//	DRV_I2C0_Initialize();

        I2C1CONbits.ON = 0;
        delay_some_iic(5);
        I2C1CONbits.ON = 1;

        delay_some(5);
	WP_SHUT();

	DRV_I2C0_MasterStart();
//        delay_some(5);
	DRV_I2C0_WaitForStartComplete();

	DRV_I2C0_ByteWrite(slave_temp);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//        delay_some(5);
	DRV_I2C0_ByteWrite((addr >> 8) & 0x00ff);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//        delay_some(5);
	DRV_I2C0_ByteWrite((addr >> 0) & 0x00ff);
	DRV_I2C0_WaitForByteWriteToComplete();

	DRV_I2C0_WriteByteAcknowledged();
//        delay_some(5);
        DRV_I2C0_MasterRestart();
//	DRV_I2C0_MasterStart();
	DRV_I2C0_WaitForStartComplete();
//        delay_some(5);
	DRV_I2C0_ByteWrite(slave_temp | 0x01);
//	DRV_I2C0_WaitForByteWriteToComplete();
//
//	DRV_I2C0_WriteByteAcknowledged();
//        delay_some(5);

        while(I2C1STATbits.TRSTAT);
	if(I2C1STATbits.ACKSTAT==1)//判断是否已发送完毕,从器件是否响应
	 	return ;
//        i = 0;
//        while((!DRV_I2C0_WaitForReadByteAvailable() && (i <= 0x0100))){        // Wait till RBF = 1; Which means data is available in I2C2RCV reg
//            i++;
//        }
         data[0] = master_readI2C1();
//            data[0] = DRV_I2C0_ByteRead();
        DRV_I2C0_MasterNACKSend();

//	for ( i = 0; i < nb; i += 1 ) {
////		DRV_I2C0_SetUpByteRead();                   //Set Rx enable in MSTR which causes SLAVE to send data
//		while(!DRV_I2C0_WaitForReadByteAvailable());        // Wait till RBF = 1; Which means data is available in I2C2RCV reg
//		data[i] = DRV_I2C0_ByteRead();
//		if(i != (nb - 1)){
//			DRV_I2C0_WriteByteAcknowledged();
//		}else{
//			DRV_I2C0_MasterNACKSend();                     //last byte; send NACK to Slave, no more data needed
//		}
//                delay_some(5);
//
//	}


	while (!DRV_I2C0_WaitForACKOrNACKComplete());
//	DRV_I2C0_WaitForACKOrNACKComplete();  		// wait till NACK sequence is complete i.e ACKEN = 0



//        delay_some(5);
	DRV_I2C0_MasterStop();

	WP_OPEN();


	return ;
}		/* -----  end of function fm24w256_selective_read  ----- */

